;+
; NAME:
;       OMNI_DML_EBAR
;
; PURPOSE:
;       Function for use with TNMIN to determine the
;       maximum-likelihood error bars for posterior DPDFs
;
; CATEGORY:
;       distance-omnibus DPDF Utility
;
; CALLING SEQUENCE:
;       diff = OMNI_DML_EBAR( level, DPDF=dpdf )
;
; INPUTS:
;       LEVEL -- From TNMIN
;
; OPTIONAL INPUTS:
;       DPDF -- Function to compute for.
;
; KEYWORD PARAMETERS:
;       NONE
;
; OUTPUTS:
;       DIFF -- Value minimized by TNMIN
;
; OPTIONAL OUTPUTS:
;       NONE
;
; MODIFICATION HISTORY:
;
;       Created:  03/06/13, TPEB -- Initial version, ported over from
;                                   RUN_MORPH_MATCHING.pro.
;
;-

FUNCTION OMNI_DML_EBAR, level, DPDF=dpdf
  
  COMPILE_OPT IDL2, HIDDEN

  ON_ERROR,2
  
  ind = WHERE(dpdf GE level[0], nind)
  area = (nind EQ 0) ? 0.d : TOTAL(dpdf[min(ind):max(ind)])
  
  ;; print,'Returning: ',abs(area/TOTAL(dpdf) - 0.6826d)
  
  RETURN, abs(area/TOTAL(dpdf) - 0.6826d)
END
